/*
 * @(#)IDFactory
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;


/**
IDFactory

This class is the base bean class object.

  @author          Ryan Kwna
  @company         DCIVision Limited
  @creation date   12/12/2005
  @version         $Revision: 1.4 $
*/
public class IDFactory {
  private static IDFactory idFactory=null;
	
  private String[] companyCodeTables=null;
  private String companyCode = "";
	
  public IDFactory(){
    String tables = SystemParameterFactory.getSystemParameter(SystemParameterConstant.SYSTEM_ID_GENERATE_BY_COMPANY_CODE);
    companyCode = SystemParameterFactory.getSystemParameter(SystemParameterConstant.COMPANY_CODE);
    companyCodeTables = TextUtility.splitString(tables,"|");
  }

  /**
   * @return
   */
  public synchronized static IDFactory getInstance() {
    if(idFactory==null){
      idFactory = new IDFactory();			
    }
    return idFactory;
  }

  public Integer getNextSequence(String tableName) throws ApplicationException{
    Integer id = null;
    if(companyCodeTables!=null){
      for(int i=0;i<companyCodeTables.length;i++){
        if(companyCodeTables[i].equals(tableName)){
          id = DataSourceFactory.getNextSequence(tableName+"_"+companyCode);
          break;
        }
      }
    }
    if(id==null) id= DataSourceFactory.getNextSequence(tableName);
    return id;
  }
}
